<mx:Module xmlns:fx="http://ns.adobe.com/mxml/2009" 
		   xmlns:s="library://ns.adobe.com/flex/spark" 
		   xmlns:mx="library://ns.adobe.com/flex/mx" layout="absolute" width="400" height="300" 
		   xmlns:core="org.flexqueries2.core.*" 
		   xmlns:fields="org.flexqueries2.fields.*" 
		   xmlns:buttons="org.flexqueries2.components.buttons.*"
		   creationComplete="module1_creationCompleteHandler(event)">

	<fx:Script>
		<![CDATA[
			import mx.events.FlexEvent;
			import org.flexqueries2.components.ModuleWindow;
			import org.flexqueries2.core.events.SqlEvent;
			
			protected function module1_creationCompleteHandler(event:FlexEvent):void
			{
				//Check if we're loading a record
				var prm:Object = ModuleWindow.getParametersFor(this); 
				if(prm != null)
				{
					//Load it !
					qLoad.sqlParams = [prm];
					qLoad.selectAsync();
				}
			}
			
			protected function btnClose_clickHandler(event:MouseEvent):void
			{
				ModuleWindow.closeWindowFor(this);
			}
			
			/**
			 * Save the record
			 */
			protected function btnSave_clickHandler(event:MouseEvent):void
			{
				if(Validator.validateAll([{foreach $cols col}val{$col}, {/foreach}]).length > 0)
					return;
				
				var isInsertion:Boolean = txt_THE_ID.text == ''; 
				
				//Is it an insertion ? let's get an ID for this row
				if(isInsertion)
					txt_THE_ID.text = SqlQuery.getId('{$table}', 'id').toString();
				
				if(isInsertion)
				{
					qInsert.sqlParams = [
						{foreach $cols col}txt{$col}.text,
						{/foreach}];
					qInsert.execAsync();
					return;
				}
				else
				{
					qUpdate.sqlParams = [
						{foreach $cols col}txt{$col}.text,
						{/foreach}];
					qUpdate.execAsync();
					return;
				}
			}
			
			/**
			 * After insertion or update
			 */
			protected function executeCompleteHandler(event:SqlEvent):void
			{
				//Check if the row was inserted or updated
				var q:SqlQuery = event.currentTarget as SqlQuery;
				if(q.affectedRows > 0)
					ModuleWindow.closeWindowFor(this);
			}
			
		]]>
	</fx:Script>

	<fx:Declarations>
	
		<!-- SqlQueries (insert, update, load) -->
		<core:SqlQuery id="qInsert" sql="insert_{$table}" executeComplete="executeCompleteHandler(event)"/>
		<!-- add this to queries.xml : <query id="insert_{$table}">{$insert}</query> -->
		
		<core:SqlQuery id="qUpdate" sql="update_{$table}" executeComplete="executeCompleteHandler(event)"/>
		<!-- add this to queries.xml : <query id="update_{$table}">{$update}</query> -->
		
		<core:SqlQuery id="qLoad" autoReadFirstRow="true" sql="get_one_{$table}"/>
		<!-- add this to queries.xml : <query id="get_one_{$table}">{$select}</query> -->
		
		<!-- Validators -->
		{foreach $cols col}
<mx:Validator id="val{$col}" required="true" source="{ txt{$col} }" property="text"/>
		{/foreach}
	</fx:Declarations>
	
	<mx:Form width="100%" top="0" bottom="35">
		{foreach $cols col}
<mx:FormItem label="{$col}" width="100%">
			<fields:SqlTextInput id="txt{$col}" query="{ qLoad }" fieldName="{$col}" width="100%"/>
		</mx:FormItem>
		{/foreach}
	</mx:Form>
	<s:HGroup height="25" bottom="5" verticalAlign="middle" textAlign="left" horizontalAlign="right" right="5" left="5">
		<buttons:ButtonSave id="btnSave" click="btnSave_clickHandler(event)"/>
		<buttons:ButtonClose id="btnClose" click="btnClose_clickHandler(event)"/>
	</s:HGroup>
</mx:Module>